package friend

import (
	"context"
	"errors"
	"gorm.io/gorm"
	Mysql "selfService/Databases"
	"selfService/好友相关/models"
)

type FriendServiceImpl struct {
}

// 根据id获得好友id列表
func (this *FriendServiceImpl)GetFriends(c context.Context,re *GetFriendsRequest) (rep *GetFriendsResponse,err error){
	var friend_ids []string
	if err=Mysql.DB.Model(models.FriendRelation{}).Select("friend_id").Where("host_id = ?", re.UserId).Find(&friend_ids).Error;err!= nil{
		return nil,err
	}
	var friend_ids2 []string
	if err=Mysql.DB.Model(models.FriendRelation{}).Select("host_id").Where("friend_id = ?", re.UserId).Find(&friend_ids2).Error;err!= nil{
		return nil,err
	}

	return &GetFriendsResponse{
		UserId: append(friend_ids,friend_ids2...),
	},nil
}

// 根据id获得申请人id列表
func (this *FriendServiceImpl)GetApply(c context.Context,re *GetApplyRequest) (rep *GetApplyResponse,err error) {
	var apply_ids []string
	if err=Mysql.DB.Model(models.FriendApply{}).Select("host_id").Where("apply_id = ? and state = 2", re.UserId).Find(&apply_ids).Error;err!= nil{
		return nil,err
	}
	return &GetApplyResponse{
		UserId: apply_ids,
	},nil
}

// 根据id 同意申请
func (this *FriendServiceImpl)AgreeApply(c context.Context,re *AgreeApplyRequest) (rep *AgreeApplyResponse,err error)  {
	err = Mysql.DB.Transaction(func(tx *gorm.DB) error {
		//好友申请置为同意
		update := tx.Model(&models.FriendApply{}).Where("host_id = ? AND apply_id = ?", re.UserId1, re.UserId2).Update("state", "1")
		if update.RowsAffected == 0 {
			return errors.New("申请关系不存在")
		}
		if update.Error != nil{
			return update.Error
		}

		//创建好友关系
		if err = tx.Create(&models.FriendRelation{HostId: re.UserId1,FriendId: re.UserId2}).Error;err != nil{
			return err
		}
		// 返回 nil 提交事务
		return nil
	})
	if err != nil {
		return &AgreeApplyResponse{
			Code: 500,
		},err
	}
	return &AgreeApplyResponse{
		Code: 200,
	},nil
}

// 根据id 拒绝申请
func (this *FriendServiceImpl)RefuseApply(c context.Context,re *RefuseApplyRequest) (rep *RefuseApplyResponse,err error){
	if err=Mysql.DB.Model(models.FriendApply{}).Where("host_id = ? AND apply_id = ?",re.UserId1,re.UserId2).Update("state","0").Error;err!= nil{
		return &RefuseApplyResponse{
			Code: 500,
		},err
	}
	return &RefuseApplyResponse{
		Code: 200,
	},nil
}

// 传⼊id 1和2 进⾏好友添加
func (this *FriendServiceImpl)CreateApply(c context.Context,re *CreateApplyRequest) (rep *CreateApplyResponse,err error){
	// 当好友关系存在时  返回错误
	var friend_ids []string
	if err=Mysql.DB.Model(models.FriendRelation{}).Select("friend_id").Where("host_id = ?  and friend_id =?", re.UserId1,re.UserId2).Find(&friend_ids).Error;err!= nil{
		return nil,err
	}
	var friend_ids2 []string
	if err=Mysql.DB.Model(models.FriendRelation{}).Select("host_id").Where("friend_id = ? and host_id = ?", re.UserId1,re.UserId2).Find(&friend_ids2).Error;err!= nil{
		return nil,err
	}
	if len(friend_ids2) + len(friend_ids)!=0 {
		return &CreateApplyResponse{
			Code: 500,
		},err
	}

	if err=Mysql.DB.Create(&models.FriendApply{HostId: re.UserId1,ApplyId: re.UserId2,State: 2}).Error;err!= nil{
		return &CreateApplyResponse{
			Code: 500,
		},err
	}
	return &CreateApplyResponse{
		Code: 200,
	},nil
}

// 传入id  删除一段好友关系
func (this *FriendServiceImpl)DeleteFriend(c context.Context,de *DeleteFriendRequese) (dep *DeleteFriendResponse, err error){
	del := Mysql.DB.Where("(host_id = ? and friend_id =?) or (friend_id = ? and host_id =?)", de.HostId, de.FriendId, de.HostId, de.FriendId).Delete(&models.FriendRelation{})
	if del.Error!= nil {
		return &DeleteFriendResponse{
			State: "500",
			Message: "服务器错误",
		},err
	}
	if del.RowsAffected == 0 {
		return &DeleteFriendResponse{
			State: "500",
			Message: "好友不存在",
		},err
	}
	return &DeleteFriendResponse{
		State: "200",
		Message: "删除成功",
	},nil
}
